//
//  LXMovieBriefView.m
//  cat
//
//  Created by imera on 16/1/13.
//  Copyright © 2016年 LX. All rights reserved.
//


#import "LXMovieBriefView.h"
#import "UIButton+WebCache.h"

@interface LXMovieBriefView()
/*** 视频播放UI */
@property (nonatomic, weak) UIButton *briefImageBtn;
/*** 视频标题UI */
@property (nonatomic, strong) UILabel *briefLabel;
/*** 视频播放次数图标 */
@property (nonatomic, weak) UIImageView *playNumImage;
/*** 视频播放次数数字 */
@property (nonatomic, weak) UILabel *playNumLabel;
/*** 视频评论次数图标 */
@property (nonatomic, weak) UIImageView *commentNumImage;
/*** 视频评论次数文字 */
@property (nonatomic, weak) UILabel *commentNumLabel;
@end

@implementation LXMovieBriefView

- (void)setVideo:(LXVideo *)video
{
    if (video != nil) {
        _video = video;
        //监听视频的播放量
//        [self.video addObserver:self forKeyPath:@"viewcounts" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    }
    
    [self layoutSubviews];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubViews];
    }
    return self;
}

//#pragma mark - 监听视频播放量
//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
//{
//    if ([keyPath isEqualToString:@"viewcounts"]) {
//        self.playNumLabel.text = [NSString stringWithFormat:@"%ld",self.video.viewcounts];
//    }
//    [self layoutSubviews];
//}

/**
 *  初始化子控件
 */
- (void)initSubViews
{
    UIButton *briefImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [briefImageBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:briefImageBtn];
    self.briefImageBtn = briefImageBtn;
    
    UILabel *briefLabel = [[UILabel alloc]init];
    briefLabel.font = [UIFont systemFontOfSize:12];
    self.briefLabel = briefLabel;
    [self addSubview:briefLabel];
    
    UIImageView *playNumImage = [[UIImageView alloc]init];
    playNumImage.image = [UIImage imageNamed:@"forwardIcon"];
    self.playNumImage = playNumImage;
    [self addSubview:playNumImage];
    
    UILabel *playNumLabel = [[UILabel alloc]init];
    playNumLabel.font = LXMovieBriefViewPlayNumFont;
    playNumLabel.textColor = [UIColor blackColor];
    self.playNumLabel = playNumLabel;
    [self addSubview:playNumLabel];
    
    UIImageView *commentNumImage = [[UIImageView alloc]init];
    commentNumImage.image = [UIImage imageNamed:@"comment_keyboard_icon"];
    self.commentNumImage = commentNumImage;
    [self addSubview:commentNumImage];
    
    UILabel *commentNumLabel = [[UILabel alloc]init];
    commentNumLabel.font = LXMovieBriefViewPlayNumFont;
    commentNumLabel.textColor = [UIColor blackColor];
    self.commentNumLabel = commentNumLabel;
    [self addSubview:commentNumLabel];

}

/**
 *  视频被点击
 */
- (void)btnClick
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"moviePlay" object:nil userInfo:@{@"video":self.video}];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    /*** 视频 */
    UIButton *briefImageBtn = self.briefImageBtn;
    CGFloat briefImageX = 0;
    CGFloat briefImageY = 0;
    CGFloat briefImageW = self.frame.size.width;
    CGFloat briefImageH = 120;
    briefImageBtn.frame = CGRectMake(briefImageX, briefImageY, briefImageW, briefImageH);
    [self.briefImageBtn sd_setImageWithURL:[NSURL URLWithString:self.video.thumb] forState:UIControlStateNormal];
    
    /*** 视频标题 */
    UILabel *briefLabel = self.briefLabel;
    CGFloat briefLabelX = briefImageX + 2;
    CGFloat briefLabelY = CGRectGetMaxY(briefImageBtn.frame) + LXMovieBriefViewMargin;
    CGFloat briefLabelW = briefImageW;
    CGFloat briefLabelH = 20;
    briefLabel.frame = CGRectMake(briefLabelX, briefLabelY, briefLabelW, briefLabelH);
    self.briefLabel.text = [NSString stringWithFormat:@"%@",self.video.fname];

    /*** 播放量图标 */
    UIImageView *playNumImage = self.playNumImage;
    playNumImage.image = [UIImage imageNamed:@"watch"];
    CGFloat playNumImageX = briefLabelX + 5;
    CGFloat playNumImageY = CGRectGetMaxY(briefLabel.frame) + LXMovieBriefViewMargin;
    CGFloat playNumImageW = 15;
    CGFloat playNumImageH = 15;
    playNumImage.frame = CGRectMake(playNumImageX, playNumImageY, playNumImageW, playNumImageH);
    
    /*** 播放量Label */
    UILabel *playNumLabel = self.playNumLabel;
    self.playNumLabel.text = [NSString stringWithFormat:@"%ld",self.video.viewcounts];
    CGSize playNumSize = [playNumLabel.text sizeWithAttributes:@{NSFontAttributeName:LXMovieBriefViewPlayNumFont}];
    CGFloat playNumLabelX = CGRectGetMaxX(playNumImage.frame) + LXMovieBriefViewMargin;
    CGFloat playNumLabelY = playNumImageY;
    CGFloat playNumLabelW = playNumSize.width;
    CGFloat playNumLabelH = playNumImageH;
    playNumLabel.frame = CGRectMake(playNumLabelX, playNumLabelY, playNumLabelW, playNumLabelH);
    
    /*** 评论数图标 */
    UIImageView *commentNumImage = self.commentNumImage;
    commentNumImage.image = [UIImage imageNamed:@"speech-bubble-center-2"];
    CGFloat commentNumImageX = CGRectGetMaxX(playNumLabel.frame) + LXMovieBriefViewMargin;
    CGFloat commentNumImageY = playNumImageY;
    CGFloat commentNumImageW = playNumImageW;
    CGFloat commentNumImageH = playNumImageH;
    commentNumImage.frame = CGRectMake(commentNumImageX, commentNumImageY, commentNumImageW, commentNumImageH);
    
    /*** 评论数Label */
    UILabel *commentNumLabel = self.commentNumLabel;
    self.commentNumLabel.text = [NSString stringWithFormat:@"%ld",self.	video.videocomments];
    CGSize commentNumSize = [commentNumLabel.text sizeWithAttributes:@{NSFontAttributeName:LXMovieBriefViewPlayNumFont}];
    CGFloat commentNumLabelX = CGRectGetMaxX(commentNumImage.frame) + LXMovieBriefViewMargin;
    CGFloat commentNumLabelY = playNumImageY;
    CGFloat commentNumLabelW = commentNumSize.width;
    CGFloat commentNumLabelH = playNumImageH;
    commentNumLabel.frame = CGRectMake(commentNumLabelX, commentNumLabelY, commentNumLabelW, commentNumLabelH);
    
}

//#pragma mark - 视图销毁
//- (void)dealloc
//{
//    [self.video removeObserver:self forKeyPath:@"viewcounts"];
//}
@end
